package com.cn.kabalifeuser.ui.service.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.ui.my.bean.CooperationSreviceListBean;
import com.cn.kabalifeuser.ui.service.adapter.TypeLeftBusinessAdapter;
import com.cn.kabalifeuser.ui.service.adapter.TypeRightBusinessAdapter;
import com.cn.kabalifeuser.utils.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/11.
 * function: 分类
 */
public class TypeBusinessActivity extends IBaseActivity<TypeBusinessView, TypeBusinessPresenter> implements TypeBusinessView {
    @BindView(R.id.rv_data_left)
    RecyclerView rvDataLeft;
    @BindView(R.id.rv_data_rigth)
    RecyclerView rvDataRigth;


    List<CooperationSreviceListBean.ResultBean> mListLeft;
    TypeLeftBusinessAdapter typeLeftAdapter;


    List<CooperationSreviceListBean.ResultBean.ChildrenBean> mListRight;
    TypeRightBusinessAdapter typeRightAdapter;


    Bundle bundle;
    private int cityID;
    private int areaID;
    private int id;

    @Override
    protected TypeBusinessPresenter createPresenter() {
        return new TypeBusinessPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_type_all;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("分类");
        bundle = getIntent().getExtras();
        cityID = bundle.getInt("cityID");
        areaID = bundle.getInt("areaID");
        id=bundle.getInt("id");
        mListLeft = new ArrayList<>();
        LinearLayoutManager layoutManager2 = new LinearLayoutManager(getMe());
        layoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager2.setSmoothScrollbarEnabled(true);
        layoutManager2.setAutoMeasureEnabled(true);
        rvDataLeft.setLayoutManager(layoutManager2);
        rvDataLeft.setNestedScrollingEnabled(false);
        //创建适配器
        typeLeftAdapter = new TypeLeftBusinessAdapter(R.layout.type_left_item, mListLeft);
        //给RecyclerView设置适配器
        rvDataLeft.setAdapter(typeLeftAdapter);
        typeLeftAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (int i = 0; i < mListLeft.size(); i++) {
                    if (i == position) {
                        mListLeft.get(i).setCheck(true);
                    } else {
                        mListLeft.get(i).setCheck(false);
                    }
                }
                typeLeftAdapter.notifyDataSetChanged();
                typeRightAdapter.replaceData(typeLeftAdapter.getItem(position).getChildren());

            }
        });
        //右边
        mListRight = new ArrayList<>();
        rvDataRigth.setLayoutManager(new GridLayoutManager(getMe(), 2));
        rvDataRigth.setNestedScrollingEnabled(false);
        //创建适配器
        typeRightAdapter = new TypeRightBusinessAdapter(R.layout.type_right_item, mListRight);
        //给RecyclerView设置适配器
        rvDataRigth.setAdapter(typeRightAdapter);
        typeRightAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt("sign", 1);
                bundle.putInt("service_type", typeRightAdapter.getItem(position).getId());
                bundle.putInt("cityID", cityID);
                bundle.putInt("areaID", areaID);
                $startActivity(MerchantsListActivity.class, bundle);
            }
        });

        mPresenter.onCooperationSreviceListData();
    }


    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onCooperationSreviceListSuccess(CooperationSreviceListBean bean) {


        for (int i = 0; i < bean.getResult().size(); i++) {
            if (id==bean.getResult().get(i).getId()){
                bean.getResult().get(i).setCheck(true);
                typeLeftAdapter.replaceData(bean.getResult());
                typeRightAdapter.replaceData(bean.getResult().get(i).getChildren());
            }
        }


    }


    @OnClick(R.id.ll_search)
    public void onViewClicked() {
        //和商户跳转，
        Bundle bundle = new Bundle();
        bundle.putInt("sign", 0);
        bundle.putInt("cityID", cityID);
        bundle.putInt("areaID", areaID);
        $startActivity(MerchantsListActivity.class, bundle);
    }
}
